package com.abu.edums.service.impl;

import com.abu.edums.mapper.VideoMapper;
import com.abu.pojo.Chapter;
import com.abu.edums.mapper.ChapterMapper;
import com.abu.pojo.Video;
import com.abu.vo.ChapterVO;
import com.abu.vo.VideoVo;
import com.abu.edums.service.ChapterService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author 阿布
 * @since 2020-06-18
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

    @Autowired
    private ChapterMapper chapterMapper;

    @Autowired
    private VideoMapper videoMapper;

    @Override
    public List<ChapterVO> queryChapterAndBar(String courseId) {
//        1.根据课程id查询出所有的章节
        List<Chapter> chapterList = this.chapterMapper.selectList(new QueryWrapper<Chapter>().eq("course_id", courseId));
//        2.根据课程id和章节id查询出所有的小节并转换
        List<ChapterVO> chapterVOList = chapterList.stream().map(chapter -> {
            ChapterVO chapterVO = new ChapterVO();
//            设置章节信息
            chapterVO.setId(chapter.getId());
            chapterVO.setTitle(chapter.getTitle());
//            获取章节id
            String chapterId = chapter.getId();
//            根据课程id和章节id获取小节
            List<Video> videoList = this.videoMapper.selectList(new QueryWrapper<Video>().eq("course_id", courseId).eq("chapter_id", chapterId));
            List<VideoVo> videoVoList = videoList.stream().map(video -> {
                VideoVo videoVo = new VideoVo();
                videoVo.setId(video.getId());
                videoVo.setTitle(video.getTitle());
                videoVo.setVideoSourceId(video.getVideoSourceId());
                return videoVo;
            }).collect(Collectors.toList());
//            设置小节信息
            chapterVO.setChildren(videoVoList);
            return chapterVO;
        }).collect(Collectors.toList());
//        3.将集合进行转换
        return chapterVOList;
    }
}
